// yb_mingpian/pages/edit/edit.js
//获取应用实例
var t = getApp(),
  a = t.requirejs("core"),
  s = t.requirejs("wxParse/wxParse");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: '../../static/images/1234.png',
    have_card: '',
    name: '',
    phone: '',
    email: '',
    company: '',
    positions: '',
    license: '',
    profile: '',
    head_photo: '',

    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  Inputname (e) {
    this.setData({
      name: e.detail.value
    })
    // console.log(this.data.name);
  },
  Inputphone (e) {
    this.setData({
      phone: e.detail.value
    })
    // console.log(this.data.phone);
  },
  Inputemail (e) {
    this.setData({
      email: e.detail.value
    })
    // console.log(this.data.email);
  },
  Inputcompany (e) {
    this.setData({
      company: e.detail.value
    })
    // console.log(this.data.company);
  },
  Inputpositions (e) {
    this.setData({
      positions: e.detail.value
    })
    // console.log(this.data.positions);
  },
  Inputprofile (e) {
    this.setData({
      profile: e.detail.value
    })
    // console.log(this.data.profile);
  },
  /**
  * 图片上传
  * this
  * n文件夹名称
  */
  chooseImageTap1: function (e) {
    var id = a.pdata(e).id;
    this.upload(this, id, 'chat_pic');
  },
  chooseImageTap2: function (e) {
    var id1 = a.pdata(e).id1;
    this.upload1(this, id1, 'chat_pic1');
  },
  upload: function (that, field, n) {
    var _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album', that, field, n)
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera', that, field, n)
          }

        }
      }
    })
  },
  upload1: function (that, field, n) {
    var _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage1('album', that, field, n)
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage1('camera', that, field, n)
          }

        }
      }
    })
  },
  chooseWxImage: function (type_, that, field, n) {
    var _this = this,
      site = getApp().siteInfo,
      url = site.siteroot + '?i=' + site.uniacid + '&t=undefined&v=' + site.version + '&from=wxapp&c=entry&a=wxapp&do=index_uploadFile&path=' + n + '&m=yb_mingpian&sign=5201314';
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type_],
      success: function (res) {
        console.log(res)
        wx.uploadFile({
          url: url,
          filePath: res.tempFilePaths[0],
          name: 'file_upload',
          success: function (res) {
            if (res.data != null && res.data != '') {
              console.log('上传头像成功', res.data)
              that.setData({
                head_photo: res.data,
              })
              // that.bindconfirm();
            } else {
              a.error('上传失败，请重试')
            }
          }
        })

      }
    })
  },
  chooseWxImage1: function (type_, that, field, n) {
    var _this = this,
      site = getApp().siteInfo,
      url = site.siteroot + '?i=' + site.uniacid + '&t=undefined&v=' + site.version + '&from=wxapp&c=entry&a=wxapp&do=index_uploadFile&path=' + n + '&m=yb_mingpian&sign=5201314';
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type_],
      success: function (res) {

        console.log(res)
        wx.uploadFile({
          url: url,
          filePath: res.tempFilePaths[0],
          name: 'file_upload',
          success: function (res) {

            if (res.data != null && res.data != '') {
              console.log(res.data)
              that.setData({
                license: res.data,
              })
              // that.bindconfirm();
            } else {
              a.error('上传失败，请重试')
            }
          }
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.canIUse);
    wx.getSetting({
      success (res) {
        console.log('asdasdasddsasadsadad', res)
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success () {
              wx.getUserInfo({
                success: (result) => {
                  console.log('result...111111111111111111111111111111', result)
                },
              });
            }
          })
        }
      }
    })

    //获取我的名片
    var o = this
    a.get("Card/GetMycard", {
      uid: t.getCache("userinfo").uid,
    }, function (res) {
      // console.log(res);
      o.setData({
        name: res.info.user_name
      })
      o.setData({
        phone: res.info.phone
      })
      o.setData({
        email: res.info.email
      })
      o.setData({
        company: res.info.company
      })
      o.setData({
        positions: res.info.positions
      })
      o.setData({
        profile: res.info.profile
      })
      o.setData({
        license: res.info.license
      })
      o.setData({
        head_photo: res.info.head_photo
      })
      //  console.log(o.data.have_card.user_name);
    })

    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  creatCard (e) {
    // let data = {
    //   uid: t.getCache("userinfo").uid,
    //   name: this.data.name,
    //   head_photo: this.data.head_photo,
    // }
    // if (!data.name || !data.head_photo) {
    //   wx.showToast({
    //     title: "请填写会员昵称~或请选择会员头像~",
    //     icon: 'none',
    //     duration: 3000
    //   })
    //   return
    // }
     let data = {
      uid: t.getCache("userinfo").uid,
      name: e.detail.userInfo.nickName,
      head_photo: e.detail.userInfo.avatarUrl,
    }
    a.get("Card/createBaseCard", data, res => {
      console.log('查看创建的名片', res)
      wx.showToast({
        title: res.info,
        icon: 'error',
      })
      if (res.code == 0) {
        wx.switchTab({
          url: '/yb_mingpian/pages/card/card',
        });
      }
    })
  },

  //更新
  upMycard: function () {
    var o = this
    console.log(o.data.name);
    console.log(o.data.phone);
    console.log(o.data.email);
    console.log(o.data.company);
    console.log(o.data.positions);
    console.log(o.data.profile);
    console.log(o.data.license);
    a.get("Card/UpMycard", {
      uid: t.getCache("userinfo").uid,
      name: o.data.name,
      phone: o.data.phone,
      email: o.data.email,
      company: o.data.company,
      positions: o.data.positions,
      profile: o.data.profile,
      license: o.data.license,
      head_photo: o.data.head_photo
    }, function (res) {
      console.log(res)
      wx.showToast({
        title: res.info,
        icon: 'none',
      })
      // setTimeout(function () {
      wx.switchTab({
        url: '/yb_mingpian/pages/cardinfo/index',
      })
      // }.bind(o), 200)
      console.log(res)
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})